package org.mspring.mlog.web.admin;

import org.mspring.mlog.web.BaseAdminController;
import org.mspring.mlog.entity.NodeEntity;
import org.mspring.mlog.entity.RoleNodeEntity;
import org.mspring.mlog.common.constants.NodeType;
import org.mspring.mlog.service.CommentEntityService;
import org.mspring.mlog.service.NodeEntityService;
import org.mspring.mlog.service.RoleNodeEntityService;
import org.mspring.nbee.web.support.WebContext;
import org.mspring.nbee.web.view.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-02 14:23
 */
@Controller
@RequestMapping("/admin/tools")
public class ToolsController extends BaseAdminController {
    private NodeEntityService nodeEntityService;
    private RoleNodeEntityService roleNodeEntityService;
    private CommentEntityService commentEntityService;

    @RequestMapping("/init-node")
    @ResponseBody
    public JsonResult initNode(WebContext context) {
        roleNodeEntityService.deleteAll();
        nodeEntityService.deleteAll();

        List<NodeEntity> nodes = new ArrayList<>();
        nodes.add(createNode("内容", "", "100", "0", NodeType.TREE_FOLDER, true, true));
        nodes.add(createNode("文章管理", "", "100105", "100", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("列表", "/admin/article/list", "100105005", "100105", NodeType.TAB, true, true));
        nodes.add(createNode("添加(markdown)", "/admin/article/create/markdown", "100105010", "100105", NodeType.TAB, false, true));
        nodes.add(createNode("添加(html)", "/admin/article/create/html", "100105015", "100105", NodeType.TAB, false, true));
        nodes.add(createNode("修改", "/admin/article/edit", "100105020", "100105", NodeType.TAB, false, false));

        nodes.add(createNode("分类管理", "", "100110", "100", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("列表", "/admin/category/list", "100110005", "100110", NodeType.TAB, true, true));
        nodes.add(createNode("添加", "/admin/category/create", "100110010", "100110", NodeType.TAB, false, true));
        nodes.add(createNode("修改", "/admin/category/edit", "100110015", "100110", NodeType.TAB, false, false));

        nodes.add(createNode("标签管理", "", "100115", "100", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("列表", "/admin/tag/list", "100115005", "100115", NodeType.TAB, true, true));
        nodes.add(createNode("添加", "/admin/tag/create", "100115010", "100115", NodeType.TAB, false, true));
        nodes.add(createNode("修改", "/admin/tag/edit", "100115015", "100115", NodeType.TAB, false, false));

        nodes.add(createNode("评论管理", "", "100120", "100", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("列表", "/admin/comment/list", "100120005", "100120", NodeType.TAB, true, true));
        nodes.add(createNode("已删除", "/admin/comment/delete/list", "100120010", "100120", NodeType.TAB, false, true));
        nodes.add(createNode("垃圾评论", "/admin/comment/spam/list", "100120015", "100120", NodeType.TAB, false, true));

        nodes.add(createNode("日记", "", "100125", "100", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("列表", "/admin/daily/list", "100125005", "100125", NodeType.TAB, true, true));
        nodes.add(createNode("添加", "/admin/daily/create", "100125010", "100125", NodeType.TAB, false, true));
        nodes.add(createNode("修改", "/admin/daily/edit", "100125015", "100125", NodeType.TAB, false, false));

        nodes.add(createNode("设置", "", "200", "0", NodeType.TREE_FOLDER, true, true));
        nodes.add(createNode("个人资料", "", "200105", "200", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("修改资料", "/admin/user/edit", "200105005", "200105", NodeType.TAB, true, true));
        nodes.add(createNode("修改密码", "/admin/user/password", "200105010", "200105", NodeType.TAB, false, true));

        nodes.add(createNode("通用配置", "", "200110", "200", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("通用配置", "/admin/config/base", "200110005", "200110", NodeType.TAB, true, true));

        nodes.add(createNode("友情链接", "", "200115", "200", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("列表", "/admin/link/list", "200115005", "200115", NodeType.TAB, true, true));
        nodes.add(createNode("添加", "/admin/link/create", "200115010", "200115", NodeType.TAB, false, true));
        nodes.add(createNode("修改", "/admin/link/edit", "200115015", "200115", NodeType.TAB, false, false));

        nodes.add(createNode("关于", "", "900", "0", NodeType.TREE_FOLDER, true, true));
        nodes.add(createNode("关于", "", "900905", "900", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("关于", "/admin/about", "900905005", "900905", NodeType.TAB, true, true));
        nodes.add(createNode("联系我们", "", "900910", "900", NodeType.TREE_ITEM, true, true));
        nodes.add(createNode("联系我们", "/admin/contact", "900910005", "900910", NodeType.TAB, true, true));

        for (NodeEntity node : nodes) {
            nodeEntityService.insert(node);

            RoleNodeEntity rn = new RoleNodeEntity();
            rn.setRoleId(1L);
            rn.setNodeId(node.getId());
            roleNodeEntityService.insert(rn);
        }
        return JsonResult.getJsonResult(true);
    }


    @RequestMapping("/import-comment")
    @ResponseBody
    public JsonResult importComment(WebContext context, Model model) {
        commentEntityService.importToDuoshuo();
        return JsonResult.getJsonResult(true);
    }

    private NodeEntity createNode(String name, String url, String code, String parentCode, String type, boolean open, boolean clickable) {
        NodeEntity node = new NodeEntity();
        node.setName(name);
        node.setUrl(url);
        node.setId(Long.valueOf(code));
        node.setCode(code);
        node.setParentCode(parentCode);
        node.setType(type);
        node.setOpen(open);
        node.setClickable(clickable);
        return node;
    }

    @Autowired
    public void setNodeEntityService(NodeEntityService nodeEntityService) {
        this.nodeEntityService = nodeEntityService;
    }

    @Autowired
    public void setRoleNodeEntityService(RoleNodeEntityService roleNodeEntityService) {
        this.roleNodeEntityService = roleNodeEntityService;
    }

    @Autowired
    public void setCommentEntityService(CommentEntityService commentEntityService) {
        this.commentEntityService = commentEntityService;
    }
}
